Skip to content

Conversation

@gdeluna-branch
Copy link
Contributor

Reference

SDK-XXXX -- <TITLE>.

Summary

Motivation

Type Of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Testing Instructions

cc @BranchMetrics/saas-sdk-devs for visibility.

@matter-code-review
Copy link
Contributor

matter-code-review bot commented Jul 11, 2025

Code Quality bug fix

Summary By MatterAI MatterAI logo

🔄 What Changed

This Pull Request introduces a crucial null check within the genericHTTPRequest method in BNCServerInterface.m. Previously, the code attempted to access serverResponse.data[@"error"] directly, which could lead to a crash if serverResponse.data itself was nil. The change adds a defensive check if(serverResponse.data && serverResponse.data[@"error"] != nil) to ensure serverResponse.data is not nil before attempting to access its keys. Additionally, a redundant NSLog statement and an inner BranchLogger call were removed, streamlining the error logging process.

🔍 Impact of the Change

This change significantly improves the robustness and stability of the SDK by preventing potential crashes when a network request is successful but returns a nil or unexpected data payload from the service. It ensures that an NSError object is only created and populated when valid error data is present, leading to more reliable error handling and logging. The removal of redundant logging also cleans up the codebase.

📁 Total Files Changed

1 file changed.

🧪 Test Added

No explicit tests were added or mentioned in the pull request description. It is highly recommended to add unit tests to cover scenarios where serverResponse.data is nil or does not contain an "error" key, to ensure the new null check behaves as expected and prevents crashes.

🔒 Security Vulnerabilities

No new security vulnerabilities were introduced. This change acts as a defensive programming measure, improving the stability and reliability of the application by preventing potential runtime crashes, which can indirectly contribute to a more secure and resilient system.

Tip

Quality Recommendations

  1. Add comprehensive unit tests specifically for the error handling logic introduced, covering cases where serverResponse.data is nil, serverResponse.data[@"error"] is nil, and when errorJson is malformed, to ensure robust error object creation and logging.

  2. Consider explicitly initializing underlyingError to nil before the if block where it's potentially assigned, to ensure its state is always clear, although Objective-C often handles this with default nil for objects.

Sequence Diagram

sequenceDiagram
    participant Caller as "Network Request Caller"
    participant BNCServerInterface as "BNCServerInterface"
    participant Service as "External Service"
    participant BranchLogger as "BranchLogger"

    Caller->>+BNCServerInterface: genericHTTPRequest(request, retryNumber)
    Note over BNCServerInterface: Handles network requests

    BNCServerInterface->>Service: HTTP Request (NSURLRequest)
    Service-->>BNCServerInterface: serverResponse (NSHTTPURLResponse, data, error)

    alt Network Request Successful (status 2xx)
        BNCServerInterface->>BNCServerInterface: Process serverResponse

        alt serverResponse.data exists AND serverResponse.data["error"] exists
            BNCServerInterface->>BNCServerInterface: Parse errorJson from serverResponse.data["error"]
            BNCServerInterface->>BNCServerInterface: Create NSError object (underlyingError) from errorJson
        else serverResponse.data is nil OR serverResponse.data["error"] is nil
            BNCServerInterface->>BNCServerInterface: underlyingError remains nil (or default)
        end

        BNCServerInterface->>BranchLogger: logError("Giving up on request with HTTP status code %ld", underlyingError)
        BNCServerInterface-->>-Caller: Callback with result/error
    else Network Request Failed (status non-2xx or network error)
        BNCServerInterface->>BranchLogger: logError("Giving up on request with HTTP status code %ld", underlyingError)
        BNCServerInterface-->>-Caller: Callback with network error
    end
Loading

@matter-code-review
Copy link
Contributor

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

@gdeluna-branch gdeluna-branch changed the title Add case to forward NSError object Create NSError object when error object is returned from services Jul 11, 2025
@gdeluna-branch
Copy link
Contributor Author

/matter review

Copy link
Contributor

@matter-code-review matter-code-review bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR implements error object creation from service responses, which is a good improvement. However, there are several issues that need to be addressed for production readiness.

Copy link
Collaborator

@NidhiDixit09 NidhiDixit09 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gdeluna-branch After this fix, this function will create a long url if short url creation fails. But react native code will not read this URL because error object will be non-nil.

@matter-code-review
Copy link
Contributor

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

@gdeluna-branch
Copy link
Contributor Author

@gdeluna-branch After this fix, this function will create a long url if short url creation fails. But react native code will not read this URL because error object will be non-nil.

Confirmed this behavior, we don't serve any long links in Android and I'm pretty sure we don't want long links returned anyway. Dropping the object for now on RN side, we can remove all references to long links in another PR.

@gdeluna-branch gdeluna-branch merged commit 429e923 into master Jul 18, 2025
14 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants